﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DigitalSolutionLibrary.SqlDatabase;

namespace DigitalSolutionManagement
{
    public partial class frmEditRole : BaseFormEdit
    {
        private Thread th;

        #region preprea form
        private void PrepareForm()
        {
            ParameterStock stock = new ParameterStock();
            Parameter par = new Parameter("RoleId", ItemId, SqlDbType.Int);
            stock.AddParamter(par);

            object obj = Program.dbWrapper.ExecuteStore("GetRole", stock, DatabaseWrapper.ReturnType.DATASET);
            if (obj != null)
            {
                DataSet ds = (DataSet)obj;
                txtDesc.Text = ds.Tables[0].Rows[0]["TypeDescription"].ToString();
                txtName.Text = ds.Tables[0].Rows[0]["TypeName"].ToString();
                processing1.HideProcessing();
            }
            else
            {
                processing1.HideProcessing();
                btnOK.Enabled = false;
                MessageBox.Show("Không thể kết nối đến server, vui lòng thử lại sau", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region ok press
        private void OkPress()
        {
            ParameterStock stock = new ParameterStock();
            Parameter par = new Parameter("RoleId", ItemId, SqlDbType.Int);
            stock.AddParamter(par);

            par = new Parameter("TypeName", txtName.Text, SqlDbType.NVarChar, 50);
            stock.AddParamter(par);

            par = new Parameter("TypeDesc", txtDesc.Text, SqlDbType.NVarChar, 200);
            stock.AddParamter(par);

            par = new Parameter("Message", null, SqlDbType.NVarChar, ParameterDirection.Output, 200);
            stock.AddParamter(par);

            object obj = Program.dbWrapper.ExecuteStore(ItemId > 0 ? "EditRole" : "AddRole", stock, DatabaseWrapper.ReturnType.FIRST_CELL);
            if (obj != null)
            {
                int rs = (int)obj;
                if (rs > 0)
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                else
                {
                    processing1.HideProcessing();
                    MessageBox.Show(stock.GetAtPosition(3).ValueOut.ToString(), "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                processing1.HideProcessing();
                MessageBox.Show("Không thể kết nối đến server, vui lòng thử lại sau", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        public frmEditRole(OpenMode mode)
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();

            if (mode == OpenMode.EDIT)
            {
                this.Text = formHeader1.Title = "Chỉnh sửa nhóm";
                btnOK.Text = "Lưu";
                btnOK.Type = DigitalSolutionControls.IconButton.ButtonType.SAVE;
            }
        }

        private void btnCancal_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void frmEditRole_Load(object sender, EventArgs e)
        {
            if (ItemId > 0)
            {
                processing1.ShowProcessing();
                th = new Thread(() => PrepareForm());
                th.Start();
            }
        }

        private void frmEditRole_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (th != null && th.IsAlive)
                th.Abort();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtName.Text == String.Empty)
            {
                MessageBox.Show("Vui lòng nhập tên nhóm", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return;
            }

            processing1.ShowProcessing();
            th = new Thread(() => OkPress());
            th.Start();
        }
    }
}
